/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Copyright (c): Johannes Kuhaupt
 * Licence: MIT
 */
package start;

import control.GuiManager;
import control.SettingsManager;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Johannes Kuhaupt
 */
public class BooEditor {
    
    private static int configCreateAttemps = 0;
  
    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {
       
           //Locale.setDefault(new Locale("en"));       
       
       
       java.awt.EventQueue.invokeLater(new Runnable() {

           @Override
            public void run() {
               boolean linuxGtk = false;
               
                try {
                       UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
                       linuxGtk = true;
                       
                } catch (Exception e) {
                    System.err.println("Error Setting LookAndFeel. No problem if you use windows");
                    
                }
                
                   if(!linuxGtk){
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(BooEditor.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(BooEditor.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(BooEditor.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (UnsupportedLookAndFeelException ex) {
                        Logger.getLogger(BooEditor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                   }
               
                   
                handleConfigSituation();
                startEditor();
                
                                
                
                
            }

           
        });
        
               
                
    }
    private static void handleConfigSituation() {
                if(SettingsManager.isPersonalConfigFileReadable()){
                    SettingsManager.loadPersonalConfig();
                    
                    if(SettingsManager.isPersonalConfigFileWriteable()){
                        SettingsManager.activateWriteToPersonalConfig();
                        SettingsManager.setReadonly(false);
                    }
                    else{
                        SettingsManager.setReadonly(true);
                    }
                    
                }
                else if(SettingsManager.isGlobalConfigFileReadable()){
                    SettingsManager.loadGlobalConfig();
                    if(SettingsManager.isPersonalConfigFileCreateable()){
                        SettingsManager.writeSettingsToFile(true);
                        SettingsManager.loadPersonalConfig();
                        SettingsManager.setReadonly(false);
                        SettingsManager.activateWriteToPersonalConfig();
                    }
                    else{
                        SettingsManager.setReadonly(true);
                    }
                    
                }
                
                else{
                    SettingsManager.createStandardSettings();
                    
                    if(SettingsManager.isGlobalConfigFileCreateable()){
                        SettingsManager.writeSettingsToFile(false);
  
                    }
                    
                    if(SettingsManager.isPersonalConfigFileCreateable()){
                        SettingsManager.writeSettingsToFile(true);
                    }
                    
                    configCreateAttemps++;
                    
                    if(configCreateAttemps<2){
                        handleConfigSituation();
                    }
                    else{
                        SettingsManager.setReadonly(true);
                    }
                    
                    
                }
                    
                    
               
           } 
    
    static void startEditor(){
         GuiManager gui = GuiManager.getInstance();
         gui.startGui(); 
    }
    
       
        
}
